package com.jixu.streetapp.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;


import java.util.Map;

/**
 * 获取经纬度字符串
 */
public class GetLocationStr {
    static String url = "https://restapi.amap.com/v3/geocode/geo";// 获取经纬度
    public static String[] keys = {
            "8acd4ccb2d1fd9eb429ab92a113abf33",
            "62cd75ba55bd5c89b2a236c028b7cf31",
            "615d52bffec423974c4788e8b4770f9e",
            "0d163b28ae44bec303da35ee39800260",
            "e907b28570ae6302ccc4161369b50850",
            "63141cfb0b16f6aa26ad23b5467aae13",
            "311c695580070ed8df7ac48130c6c048"
    };
    static int i = 0;

    /**
     * 带参数的get请求
     *
     * @return String
     */
    public static String doGet(String address) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String LocationStr = "";
        String resultString = "";
        CloseableHttpResponse response = null;
        JSONObject obj = null;
        try {
            // 创建uri 携带请求参数
            URIBuilder builder = new URIBuilder(url);
            // 携带地址参数
            builder.addParameter("address", address);
            builder.addParameter("key", keys[i]);
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                obj = JSON.parseObject(resultString);
                // 判断返回数据
                if (!obj.get("infocode").toString().equals("10000")) {
                    try {
                        builder.setParameter("key", keys[++i]);
                    } catch (Exception e) {
                        System.out.println("已达今日使用上线");
                    }
                    uri = builder.build();
                    httpGet = new HttpGet(uri);
                    response = httpclient.execute(httpGet);
                    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                }
                obj = JSON.parseObject(resultString);
                JSONArray objects = (JSONArray) obj.get("geocodes");
                try {
                    JSONObject item = (JSONObject) objects.get(0);
                    LocationStr = item.get("location").toString();
                } catch (Exception e) {
                    System.out.println(obj.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return LocationStr;
    }
}
